package com.orange.lockscreen.layers.widget;

import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.orange.lockscreen.R;
import com.orange.lockscreen.SlatHashMap;
import com.orange.lockscreen.common.Constants;
import com.orange.lockscreen.common.LockScreenPreferences;
import com.orange.lockscreen.common.LockScreenUtils;
import com.orange.lockscreen.common.LogHelper;
import com.orange.lockscreen.common.Constants.Hanlder_Orientation;
import com.orange.lockscreen.common.Constants.Slat_Type;
import com.orange.lockscreen.layers.GenericSlat;

public class WidgetSlat extends GenericSlat {
	private static final String			TAG					= "WidgetSlat";

	public static final int				STATE_NO_COVERED	= 0;
	public static final int				STATE_COVERED		= 1;

	private static final int			BLACK_COLOR_100		= 0xFF323232;
	private static final int			WHITE_COLOR_0		= 0x00FFFFFF;
	private static final int			WHITH_COLOR_100		= 0xFFFFFFFF;

	private View						mHostView			= null;
	private int							mRowIndex			= 0;
	private ImageView					mCoverLayer			= null;
	private ImageView					mGripLayer			= null;
	private ImageView					mLock				= null;
	private TextView					mPromptView			= null;

	private OnMusicControlTouchListener	mListener			= null;

	public WidgetSlat(Context context, int rowIndex) {
		super(context);
		mRowIndex = rowIndex;
		initUi(context);
	}

	private void initUi(Context context) {
		mCoverLayer = new ImageView(context);
		mCoverLayer.setImageResource(R.drawable.slat_mask_transparent);
		mCoverLayer.setScaleType(ScaleType.FIT_XY);
		addView(mCoverLayer);

		mGripLayer = new ImageView(context);
		mGripLayer.setImageResource(R.drawable.grip);
		addView(mGripLayer);

		mLock = new ImageView(context);
		mLock.setImageResource(R.drawable.icon_padlock_big);
		addView(mLock);

		mPromptView = new TextView(context);
		// for fix bug outofMemoryError
		mPromptView.setCursorVisible(false);
		mPromptView.setText(R.string.tip_unlock);
		mPromptView.setTextSize(14.0f);
		mPromptView.setTypeface(Typeface.SANS_SERIF);
		mPromptView.setTextColor(WHITH_COLOR_100);
		addView(mPromptView);

		invalidateUi();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		LogHelper.v(TAG, "onLayout. rowIndex=" + mRowIndex);

		if (LogHelper.enableDefaultLog) {
			if (mRowIndex == SlatHashMap.UNLOCK_ROWINDEX) {
				Configuration newConfig = getResources().getConfiguration();
				LogHelper.d("lock", "" + "newConfig.orientation" + newConfig.orientation);
			}
		}
		
		int rowHeight = SlatHashMap.getSlatHeight(mRowIndex);
		final View hostView = mHostView;
		if (hostView != null) {
			int measuredHeight = hostView.getMeasuredHeight();
			int top = (int) ((rowHeight - measuredHeight) / 2.0);
			int bottom = top + measuredHeight;
			int left = 0;
			int right = r - l;
			hostView.layout(left, top, right, bottom);
		}

		mCoverLayer.layout(0, 0, r - l, b - t);

		int top = 0;
		int bottom = 0;
		int left = 0;
		int right = 0;
		int measuredHeight = 0;
		int measuredWidth = 0;
		int handlerOri = SlatHashMap.getHandlerOrientation();
		int gap = 10;
		measuredHeight = mGripLayer.getDrawable().getIntrinsicHeight();
		measuredWidth = mGripLayer.getDrawable().getIntrinsicWidth();
		top = (int) ((rowHeight - measuredHeight) / 2.0f);
		bottom = top + measuredHeight;
		left = 0;
		if (Hanlder_Orientation.LEFT_TO_RIGHT == handlerOri) {
			left = gap;
		} else {
			left = r - l - measuredWidth - gap;
		}
		right = left + measuredWidth;
		mGripLayer.layout(left, top, right, bottom);

		int lockWidth = mLock.getDrawable().getIntrinsicWidth();
		int lockHeight = mLock.getDrawable().getIntrinsicHeight();
		int promptWidth = mPromptView.getMeasuredWidth();
		int promptHeight = mPromptView.getMeasuredHeight();
		
		/**
		 * add by gaoge 2011-11-25 for fix bug that when landscape,the lock png dispear
		 * this is because in normal,the r should be 320,the b should be 480
		 * 
		 * According to Log:
		 * Wrong Case:
		 * 1:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% l is: 0,t is: 339r is: 480,b is: 455
		 * 2:onLayout() isUnlockPromptEnabled true ; mLock left: 220,top: 25,right: 260,bottom: 65
		 * 
		 * Right Case:
		 * 1:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% l is: 0,t is: 339r is: 320,b is: 455
		 * 2:onLayout() isUnlockPromptEnabled true ; mLock left: 140,top: 25,right: 180,bottom: 65
		 */
		if(mRowIndex == SlatHashMap.UNLOCK_ROWINDEX){
			LogHelper.d("lock", "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% l is: " + l + ",t is: " + t+ "r is: " + r + ",b is: " + b);
			String sBuildModel = Build.MODEL.toLowerCase(Locale.UK);
			if (sBuildModel.contains("acer")){
				if(480 == r) r = 320;
			}
		} 
		if (LockScreenPreferences.isUnlockPromptEnabled()) {
			left = (int) ((r - l - lockWidth) / 2.0f);
			right = left + lockWidth;
			top = (int) ((rowHeight - lockHeight - promptHeight - 7) / 2.0f);
			bottom = top + lockHeight;
			mLock.layout(left, top, right, bottom);
			if (mRowIndex == SlatHashMap.UNLOCK_ROWINDEX) {
				LogHelper.d("lock", "" + "onLayout() isUnlockPromptEnabled true ; mLock left: " + left + ",top: "
						+ top + ",right: " + right + ",bottom: " + bottom);
			}
			left = (int) ((r - l - promptWidth) / 2.0f);
			right = left + promptWidth;
			top = top + lockHeight + 7; 
			bottom = top + promptHeight;
			mPromptView.layout(left, top, right, bottom);
		} else {
			left = (int) ((r - l - lockWidth) / 2.0f);
			right = left + lockWidth;
			top = (int) ((rowHeight - lockHeight) / 2.0f);
			bottom = top + lockHeight;
			mLock.layout(left, top, right, bottom);
			if (mRowIndex == SlatHashMap.UNLOCK_ROWINDEX) {
				LogHelper.d("lock", "" + "onLayout() isUnlockPromptEnabled false ; mLock left: " + left + ",top: "
						+ top + ",right: " + right + ",bottom: " + bottom); 
			}
			left = (int) ((r - l - promptWidth) / 2.0f);
			right = left + promptWidth;
			top = top + lockHeight;
			bottom = top + promptHeight;
			mPromptView.layout(left, top, right, bottom);
		}

		if (mRowIndex == SlatHashMap.MUSIC_WIDGET_ROWINDEX) {
			mGoneOnDecView = findViewWithTag("gone_on_dec");
			// If the Gone on dec view is found, find further.
			if (mGoneOnDecView != null) {
				mGoneOnCtlView = findViewWithTag("gone_on_ctl");
				mInvisibleOnCtlView = findViewWithTag("invisible_on_ctl");
			}

			if (mGoneOnDecView != null && mGoneOnDecView.isShown()) {
				responseMPBtnLongClick();
			}
		}
	}

	private View	mGoneOnDecView;
	private View	mGoneOnCtlView;
	private View	mInvisibleOnCtlView;

	public void addHostView(View hostView) {
		if (hostView != null) {
			mHostView = hostView;
			addView(hostView);
			invalidateUi();
		}
	}

	public void setMusicControlListener(OnMusicControlTouchListener listener) {
		mListener = listener;
	}

	public void changeState(int state) {
		switch (state) {
		// Dec.
		case STATE_NO_COVERED:
			mCoverLayer.setVisibility(VISIBLE);
			mGripLayer.setVisibility(VISIBLE);
			setBackgroundColor(WHITE_COLOR_0);

			if (mGoneOnDecView != null) {
				mGoneOnDecView.setVisibility(View.GONE);
			}

			if (mGoneOnCtlView != null) {
				mGoneOnCtlView.setVisibility(View.VISIBLE);
			}

			if (mInvisibleOnCtlView != null) {
				if (LockScreenUtils.isMusicTrackSelected(getContext())) {
					mInvisibleOnCtlView.setVisibility(View.VISIBLE);
				} else {
					mInvisibleOnCtlView.setVisibility(View.GONE);
				}
			}
			break;
		case STATE_COVERED:
			mCoverLayer.setVisibility(INVISIBLE);
			mGripLayer.setVisibility(INVISIBLE);
			setBackgroundColor(BLACK_COLOR_100);

			if (mGoneOnDecView != null) {
				mGoneOnDecView.setVisibility(View.VISIBLE);
			}
			if (mGoneOnCtlView != null) {
				mGoneOnCtlView.setVisibility(View.GONE);
			}
			if (mInvisibleOnCtlView != null) {
				mInvisibleOnCtlView.setVisibility(View.GONE);
			}
			break;
		default:
			break;
		}
	}

	public void invalidateUi() {
		int slatType = SlatHashMap.getSlatType(mRowIndex);
		if(SlatHashMap.UNLOCK_ROWINDEX == mRowIndex){
			LogHelper.d("lock", "********************* invalidateUi, mRowIndex :" + mRowIndex + "slatType " +  slatType);
		}
		switch (slatType) {
		case Slat_Type.TYPE_FIXED:
			if (mHostView != null) {
				mCoverLayer.setImageResource(R.drawable.slat_mask_dimmed);
				mCoverLayer.setVisibility(VISIBLE);
			} else {
				mCoverLayer.setVisibility(INVISIBLE);
			}
			mGripLayer.setVisibility(INVISIBLE);
			mLock.setVisibility(INVISIBLE);
			mPromptView.setVisibility(INVISIBLE);
			break;
		case Slat_Type.TYPE_MOVABLE:
			if (mHostView != null) {
				mCoverLayer.setImageResource(R.drawable.slat_mask_dimmed);
				mGripLayer.setVisibility(VISIBLE);
				mCoverLayer.setVisibility(VISIBLE);
			} else {
				mGripLayer.setVisibility(INVISIBLE);
				mCoverLayer.setVisibility(VISIBLE);
			}
			mLock.setVisibility(INVISIBLE);
			mPromptView.setVisibility(INVISIBLE);
			break;
		case Slat_Type.TYPE_MOVABLE_UNLOCK:
			if (mHostView != null) {
				mCoverLayer.setImageResource(R.drawable.slat_mask_dimmed);
				mCoverLayer.setVisibility(VISIBLE);
			} else {
				mCoverLayer.setVisibility(VISIBLE);
			}
			mGripLayer.setVisibility(INVISIBLE);
			mLock.setVisibility(INVISIBLE);
			mPromptView.setVisibility(INVISIBLE);
			break;
		case Slat_Type.TYPE_UNLOCK:
			mGripLayer.setVisibility(INVISIBLE);
			mCoverLayer.setImageResource(R.drawable.slat_mask_dimmed);
			mCoverLayer.setVisibility(VISIBLE);
			mLock.setVisibility(VISIBLE);
			if (LockScreenPreferences.isUnlockPromptEnabled()) {
				mPromptView.setVisibility(VISIBLE);
			} else {
				mPromptView.setVisibility(INVISIBLE);
			}
			break;
		default:
			if(SlatHashMap.UNLOCK_ROWINDEX == mRowIndex){
				LogHelper.d("lock", "********************* mRowIndex : " + mRowIndex + ",default case, set All to INVISIBLE");
			}
			mGripLayer.setVisibility(INVISIBLE);
			mCoverLayer.setVisibility(INVISIBLE);
			mLock.setVisibility(INVISIBLE);
			mPromptView.setVisibility(INVISIBLE);
			break;
		}
	}

	public void useHighlightMask() {
		mCoverLayer
				.setImageResource(R.drawable.slat_mask_transparent_highlight_only);
		mCoverLayer.setVisibility(VISIBLE);
		mGripLayer.setVisibility(INVISIBLE);
		mLock.setVisibility(INVISIBLE);
	}

	public void useShadowMask() {
		mCoverLayer
				.setImageResource(R.drawable.slat_mask_transparent_shadow_only);
		mCoverLayer.setVisibility(VISIBLE);
		mGripLayer.setVisibility(INVISIBLE);
		mLock.setVisibility(INVISIBLE);
	}

	public void useNormalMask() {
		mCoverLayer.setImageResource(R.drawable.slat_mask_transparent);
		mCoverLayer.setVisibility(VISIBLE);
		mGripLayer.setVisibility(INVISIBLE);
		mLock.setVisibility(INVISIBLE);
	}

	public void release() {
		mCoverLayer.setBackgroundDrawable(null);
		mGripLayer.setBackgroundDrawable(null);
		mLock.setBackgroundDrawable(null);
		mHostView = null;
	}

	public void responseMPBtnLongClick() {
		final ImageButton mPrevBtn = (ImageButton) findViewWithTag("mp_btn_prev");
		final ImageButton mNextBtn = (ImageButton) findViewWithTag("mp_btn_next");

		if (mPrevBtn != null) {
			mPrevBtn.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					Intent intent = new Intent();
					intent.setAction(Constants.ACTION_MUSIC_PREV_LONGCLICK);
					
					try {
						getContext().sendBroadcast(intent);
					} catch (Exception e) {
						LogHelper.e(TAG, "sendBroadcast. ", e);
						// Should use "IllegalStateException", but trying to be safe.
					}
					return true;
				}
			});

			mPrevBtn.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					int action = event.getAction();
					switch (action) {
					case MotionEvent.ACTION_DOWN:
						if (mListener != null) {
							mListener.onDown(mRowIndex);
						}
						break;
					case MotionEvent.ACTION_UP:
					case MotionEvent.ACTION_CANCEL:
						Intent intent = new Intent();
						intent.setAction(Constants.ACTION_MUSIC_PREV_TOUCH);
						intent.putExtra(Constants.KEY_MUSIC_BTN_PREV, action);
						
						try {
							getContext().sendBroadcast(intent);
						} catch (Exception e) {
							LogHelper.e(TAG, "sendBroadcast. ", e);
							// Should use "IllegalStateException", but trying to be safe.
						}
						if (mListener != null) {
							mListener.onUp(mRowIndex);
						}
						break;
					default:
						break;
					}
					return v.onTouchEvent(event);
				}
			});
		}

		if (mNextBtn != null) {
			mNextBtn.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					Intent intent = new Intent();
					intent.setAction(Constants.ACTION_MUSIC_NEXT_LONGCLICK);
					
					try {
						getContext().sendBroadcast(intent);
					} catch (Exception e) {
						LogHelper.e(TAG, "sendBroadcast. ", e);
						// Should use "IllegalStateException", but trying to be safe.
					}
					return true;
				}
			});

			mNextBtn.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					int action = event.getAction();
					switch (action) {
					case MotionEvent.ACTION_DOWN:
						if (mListener != null) {
							mListener.onDown(mRowIndex);
						}
						break;
					case MotionEvent.ACTION_UP:
					case MotionEvent.ACTION_CANCEL:
						Intent intent = new Intent();
						intent.setAction(Constants.ACTION_MUSIC_NEXT_TOUCH);
						intent.putExtra(Constants.KEY_MUSIC_BTN_NEXT, action);
						
						try {
							getContext().sendBroadcast(intent);
						} catch (Exception e) {
							LogHelper.e(TAG, "sendBroadcast. ", e);
							// Should use "IllegalStateException", but trying to be safe.
						}
						if (mListener != null) {
							mListener.onUp(mRowIndex);
						}
						break;
					default:
						break;
					}
					return v.onTouchEvent(event);
				}
			});
		}
	}

	public interface OnMusicControlTouchListener {
		public void onDown(int rowIndex);

		public void onUp(int rowIndex);
	}
}
